/*
 * Board.java
 *
 * Created on 05. Juni 2007, 02:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.repos;

import sbc.util.*;
import java.util.*;
import org.xvsm.api.core.*;
import corso.lang.*;
/**
 *
 * @author mustermark
 */
public class Sensor extends Module {
    
    private String uid;
    private EnumSet<Feature> features;
    private EnumMap<Feature, Boolean> inspectedFeatureMap;
    private Type type = Module.Type.SENSOR;
    //private EnumMap<Feature, Inspector> inspections;
    
    /** Creates a new instance of Board */
    public Sensor(String id, EnumSet<Feature> feat) {
        //super(Module.Type.SENSOR);
        uid = id;
        features = feat;
        inspectedFeatureMap = new EnumMap<Feature, Boolean>(Feature.class);
        for(Feature f : features) inspectedFeatureMap.put(f, false);
        
    }
    
    public Sensor() {}
    
    public Sensor(CorsoVarOid oid) {
        try {
            oid.setTimeStamp (0);
            oid.readShareable (this, null, CorsoConnection.NO_TIMEOUT);
        } catch (Exception e) {
            System.exit (-9);
        }
    }
    
    public Sensor(CorsoVarOid oid, CorsoTransaction tx) {
        try {
            oid.setTimeStamp (0);
            oid.readShareable (this, tx, CorsoConnection.NO_TIMEOUT);
        } catch (Exception e) {
            System.exit (-9);
        }
    }
    
    
    public String getUID() {
        return uid;
    }
    public Type getType() { return type; }
    public EnumSet<Feature> getFeatures() { return features; }
    
    public Tuple asTuple() {
        Tuple tuple = new Tuple();
        tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, type.toString()));
        tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, uid));
        for(Feature f : features) tuple.addEntry(Entry.Factory.newInstance(ValueTypes.STRING_UTF8, f.getLabel()));
        return tuple;
    }
    
     public void write (CorsoData data) throws CorsoDataException {
        data.putStructTag (getType().toString(), features.size() + 1);
        data.putString(uid);
        System.out.println("writing sensor: " + uid);
        for(Feature f : features) {
            data.putString(f.getLabel());
            System.out.println("feature " + f.getLabel());
        }
        
    }
    public void read (CorsoData data) throws CorsoDataException {
        System.out.println("reading sensor: " + uid);
        
        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);
        if (arity < 1 || arity > 3) throw new CorsoDataException ();
        if (!structName.toString ().equals (getType().toString())) {
            throw new CorsoDataException ();
        }
        features = EnumSet.noneOf(Feature.class);
        for(int i = 0; i < arity; i++) features.add(Feature.valueOf(data.getString().toUpperCase()));
        
    }
    
    public boolean isInspected(Feature f) {
        return inspectedFeatureMap.get(f);
    }
    
    public boolean isInspected() {
        boolean b = true;
        for(Feature f : features) b &= inspectedFeatureMap.get(f);
        return b;
    }
    
    public void setInspected(Feature f) {
        inspectedFeatureMap.put(f, true);
    }
}

